package com.example.yiwei.crm.readcard;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetActivity;
import com.example.yiwei.crm.data.GetPeople;
import com.example.yiwei.crm.data.GetStream;
import com.example.yiwei.crm.entity.PeopleEntity;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.view.activity.JYCoordingActivity.AddCoorCustomerActivity;
import com.ivsign.android.IDCReader.IDCReaderSDK;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.UUID;


public class MainActivitycard extends BaseActivity{

    ListView mList;
    Spinner mSpin;
    private Button scanf, read;//, colse, sleep, week;
    private ImageView ic;
    private TextView name,sex,minzu,bday,adress,idCard,qfjg,time;
    Handler mHandler = new Handler();
    BluetoothAdapter mBluetoothAdapter;
    ArrayList<String> list;
    ArrayAdapter<String> mAdapter;
    AlertDialog dialog;
    ProgressDialog pDialog;
    BluetoothSocket mBluetoothSocket;
    UUID myuuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
    static InputStream mInputStream;
    static OutputStream mOutputStream;
    GetStream getStream;
    int Readflage = -99;
    String[] decodeInfo = new String[10];
    ReadCard rCard;
    public static MainActivitycard a;
    String path, uid, depart;

    byte[] cmd_SAM = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x12, (byte) 0xFF,
            (byte) 0xEE};
    byte[] cmd_find = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x20, 0x01, 0x22};
    byte[] cmd_selt = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x20, 0x02, 0x21};
    byte[] cmd_read = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x03, 0x30, 0x01, 0x32};
    byte[] cmd_sleep = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x02, 0x00, 0x02};
    byte[] cmd_weak = {(byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0x96, 0x69, 0x00, 0x02, 0x01, 0x03};
    byte[] recData = new byte[1500];


    @Override
    protected void onResume() {
        super.onResume();
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            String action = arg1.getAction();
            if (action.equals(BluetoothDevice.ACTION_FOUND)) {
                BluetoothDevice device = arg1.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String te = device.getName() + "|" + device.getAddress();
                Log.d("aaa",te);
                if (!list.contains(te)) {
                    list.add(te);
                    mAdapter.notifyDataSetChanged();
                }
            } else if (action.equals(BluetoothAdapter.ACTION_DISCOVERY_FINISHED)) {
                // setProgressBarIndeterminateVisibility(false);
                setTitle("搜索蓝牙设备");
                Log.d("cc","aa");
            }
        }
    };

    View.OnClickListener scanfListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            list = new ArrayList<String>();
            if (mBluetoothAdapter.isDiscovering()) {
                mBluetoothAdapter.cancelDiscovery();
            }
            mBluetoothAdapter.startDiscovery();
            mAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.tv, list);
            mList.setAdapter(mAdapter);
            dialog.show();
        }
    };
    View.OnClickListener readListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            // tv.setText("");
            // ic.setImageBitmap(BitmapFactory.decodeResource(getResources(),
            // R.mipmap.face));
            // pDialog.show();

            // ic,pDialog);
            // new Thread(rCard).start();

//            tv.setText("");
            int readcount = 5;
            try {
                while (readcount > 1) {
                    ReadCard();
                    readcount = readcount - 1;
                    if (Readflage > 0) {
                        readcount = 0;
//                        tv.setText("姓名：" + decodeInfo[0] + "\n" + "性别：" + decodeInfo[1] + "\n" + "民族：" + decodeInfo[2]
//                                + "\n" + "出生日期：" + decodeInfo[3] + "\n" + "地址：" + decodeInfo[4] + "\n" + "身份号码："
//                                + decodeInfo[5] + "\n" + "签发机关：" + decodeInfo[6] + "\n" + "有效期限：" + decodeInfo[7] + "-"
//                                + decodeInfo[8] + "\n" + decodeInfo[9] + "\n");
                        name.setText(decodeInfo[0].trim());
                        sex.setText(decodeInfo[1].trim());
                        minzu.setText(decodeInfo[2].trim());
                        bday.setText(decodeInfo[3].trim());
                        adress.setText(decodeInfo[4].trim());
                        idCard.setText(decodeInfo[5].trim());
                        qfjg.setText(decodeInfo[6].trim());
                        time.setText(decodeInfo[7].trim()+ "-" + decodeInfo[8].trim());
                        GetPeople.creatpeople();
                        PeopleEntity peopleEntity = GetPeople.getpeople();
                        peopleEntity.setName(decodeInfo[0].trim());
                        peopleEntity.setSex(decodeInfo[1].trim());
                        peopleEntity.setNationality(decodeInfo[2].trim());
                        peopleEntity.setBirthday(decodeInfo[3].trim());
                        peopleEntity.setAddress(decodeInfo[4].trim());
                        peopleEntity.setIdcard(decodeInfo[5].trim());
                        if (Readflage == 1) {
                            FileInputStream fis = new FileInputStream(
                                    Environment.getExternalStorageDirectory() + "/wltlib/zp.bmp");
                            Bitmap bmp = BitmapFactory.decodeStream(fis);
                            fis.close();
                            ic.setImageBitmap(bmp);
                        } else {
                            Toast.makeText(MainActivitycard.this, "照片解码失败，请检查路径" + Environment.getExternalStorageDirectory() + "/wltlib/", Toast.LENGTH_SHORT).show();
//                            ic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.face));
                        }
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                try {
//                                    HttpClientUtil httpClient = new HttpClientUtil();
//                                    HttpClientUtil.MultipartForm form = httpClient.new MultipartForm();
//                                    //设置form属性、参数
//                                    form.setAction("http://shykyuyue.shabc.net/api/TestApp.aspx");
//                                    String path = Environment.getExternalStorageDirectory() + "/wltlib/zp.bmp";
//
//                                    File file = new File(path);
//                                    form.addFileField("photo", file);
//                                    form.addNormalField("name", decodeInfo[0]);
//                                    form.addNormalField("sex", decodeInfo[1]);
//                                    form.addNormalField("nation ", decodeInfo[2]);
//                                    form.addNormalField("birth", decodeInfo[3]);
//                                    form.addNormalField("addr", decodeInfo[4]);
//                                    form.addNormalField("code", decodeInfo[5]);
//                                    form.addNormalField("department", decodeInfo[6]);
//                                    form.addNormalField("start", decodeInfo[7]);
//                                    form.addNormalField("end", decodeInfo[8]);
//                                    form.addNormalField("uid",uid);
//                                    form.addNormalField("depart",depart);
//
//                                    String resultcode = httpClient.submitForm(form);
//                                    Looper.prepare();
//                                    if (resultcode.trim().length() == 0)
//                                        Toast.makeText(MainActivitycard.this, "已将信息同步到服务器", Toast.LENGTH_SHORT).show();
//                                    else
//                                        Toast.makeText(MainActivitycard.this, resultcode, Toast.LENGTH_SHORT).show();
//                                    Looper.loop();
//                                } catch (Exception e) {
//                                    Toast.makeText(MainActivitycard.this, e.getMessage(), Toast.LENGTH_SHORT).show();
//                                }
//                            }
//                        }).start();
                    } else {
//                        ic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.face));
                        if (Readflage == -2) {
                            Toast.makeText(MainActivitycard.this, "蓝牙连接异常", Toast.LENGTH_SHORT).show();
                        }
                        if (Readflage == -3) {
                            Toast.makeText(MainActivitycard.this, "无卡或卡片已读过", Toast.LENGTH_SHORT).show();
//                            tv.setText("无卡或卡片已读过");
                        }
                        if (Readflage == -4) {
                            Toast.makeText(MainActivitycard.this, "无卡或卡片已读过", Toast.LENGTH_SHORT).show();
//                            tv.setText("无卡或卡片已读过");
                        }
                        if (Readflage == -5) {
                            Toast.makeText(MainActivitycard.this, "读卡失败", Toast.LENGTH_SHORT).show();
//                            tv.setText("读卡失败");
                        }
                        if (Readflage == -99) {
                            Toast.makeText(MainActivitycard.this, "操作异常", Toast.LENGTH_SHORT).show();
//                            tv.setText("操作异常");
                        }
                    }
                    Thread.sleep(100);
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
//                tv.setText("读取数据异常！");
                Toast.makeText(MainActivitycard.this, "读取数据异常！", Toast.LENGTH_SHORT).show();
//                ic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.face));
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
//                tv.setText("读取数据异常！");
                Toast.makeText(MainActivitycard.this, "读取数据异常！", Toast.LENGTH_SHORT).show();
//                ic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.face));
            }
        }
    };
    View.OnClickListener colseListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try {
                if ((mInputStream == null) || (mOutputStream == null)) {
                    return;
                }
                mOutputStream.close();
                mInputStream.close();
                mBluetoothSocket.close();
                Toast.makeText(MainActivitycard.this,"已断开", Toast.LENGTH_SHORT).show();
//                tv.setText("已断开");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };
    View.OnClickListener sleepListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try {
                if ((mInputStream == null) || (mOutputStream == null)) {
//                    tv.setText("设备未正常连接！");
                    Toast.makeText(MainActivitycard.this,"设备未正常连接！", Toast.LENGTH_SHORT).show();
                    return;
                }
                mOutputStream.write(cmd_sleep);
                Toast.makeText(MainActivitycard.this,"睡眠模式！", Toast.LENGTH_SHORT).show();
//                tv.setText("睡眠模式！");
//                ic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.face));
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    };
    View.OnClickListener weekListener = new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try {
                if ((mInputStream == null) || (mOutputStream == null)) {
//                    tv.setText("设备未正常连接！");
                    Toast.makeText(MainActivitycard.this,"设备未正常连接！", Toast.LENGTH_SHORT).show();
                    return;
                }
                mOutputStream.write(cmd_weak);
//                tv.setText("唤醒模式！");
                Toast.makeText(MainActivitycard.this,"唤醒模式！", Toast.LENGTH_SHORT).show();
//                ic.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.face));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_card);
        a= this;
        uid = getIntent().getStringExtra("id");
        depart = getIntent().getStringExtra("depart");
        init();
        Listener();
        path = Environment.getExternalStorageDirectory() + "/wltlib";
        if (!new File(path + "/base.dat").exists()) {
            copy(MainActivitycard.this, "base.dat", path, "base.dat");
            Log.d("cccc","aaaaaaa");
        }
        if (!new File(path + "/license.lic").exists()) {
            copy(MainActivitycard.this, "license.lic", path, "license.lic");
            Log.d("cccc","bbbbbbb");
        }

        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (!mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable();
        }
        IntentFilter mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        registerReceiver(mReceiver, mFilter);
        Log.d("aa","cc");
        // 注册搜索完时的receiver
        mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        registerReceiver(mReceiver, mFilter);
        Log.d("aa","bb");
        pDialog = new ProgressDialog(MainActivitycard.this);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setTitle("正在读卡……");


        View view = LayoutInflater.from(MainActivitycard.this).inflate(R.layout.dialog, null);
        dialog = new AlertDialog.Builder(MainActivitycard.this, R.style.AlertDialog).setTitle("蓝牙列表(单击连接设备)").setView(view).setCancelable(false)
                .setNegativeButton("取消", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                }).create();
        mList = (ListView) view.findViewById(R.id.list);
        mList.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                // TODO Auto-generated method stub
                String text = list.get(arg2);
                // System.out.println("BBB-" + text);
                int a = text.indexOf("|");
                String mac = text.substring(a + 1);
                if (GetStream.mac==null){
                    getStream = new GetStream();
                    getStream.mac = mac;
                }
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(mac);
                try {
                    Boolean returnValue = false;
                    Method createBondMethod = device.getClass().getMethod("createBond");
                    connect(device);
                } catch (Exception e) {
                    // TODO: handle exception
                    Toast.makeText(MainActivitycard.this, "连接失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
//        /** * 设置为横屏 */
//        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
    }

    private void connect(BluetoothDevice device) {
        try {
            mBluetoothSocket = device.createRfcommSocketToServiceRecord(myuuid);
            mBluetoothSocket.connect();
            Toast.makeText(MainActivitycard.this, "连接成功", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
            getStream.mInputStream =  mBluetoothSocket.getInputStream();
            getStream.mOutputStream = mBluetoothSocket.getOutputStream();
            mInputStream = mBluetoothSocket.getInputStream();
            mOutputStream = mBluetoothSocket.getOutputStream();

        } catch (Exception e) {
            // TODO: handle exception
            Toast.makeText(MainActivitycard.this, "连接失败", Toast.LENGTH_SHORT).show();
        }

    }

    private void copy(Context mContext, String ASSETS_NAME, String PATH, String NAME) {
        // TODO Auto-generated method stub
        String filename = PATH + "/" + NAME;
        File dir = new File(PATH);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        try {
            if (!new File(filename).exists()) {
                InputStream is = mContext.getResources().getAssets().open(ASSETS_NAME);
                FileOutputStream fos = new FileOutputStream(filename);
                byte[] buf = new byte[7168];
                int count = 0;
                while ((count = is.read(buf)) > 0) {
                    fos.write(buf, 0, count);
                }
                fos.close();
                is.close();
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        unregisterReceiver(mReceiver);
        try {
            if ((mInputStream == null) || (mOutputStream == null)) {
                return;
            }
//            mOutputStream.close();
//            mInputStream.close();
//            mBluetoothSocket.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private void Listener() {
        // TODO Auto-generated method stub
        scanf.setOnClickListener(scanfListener);
        read.setOnClickListener(readListener);
        //colse.setOnClickListener(colseListener);
        //sleep.setOnClickListener(sleepListener);
        //week.setOnClickListener(weekListener);
    }

    private void init() {
        // TODO Auto-generated method stub
        getStream = GetStream.get();
        if (getStream.mac!=null){
            scanf = (Button) findViewById(R.id.button);
            scanf.setVisibility(View.GONE);
            mInputStream = getStream.mInputStream;
            mOutputStream = getStream.mOutputStream;
        }else{
            scanf = (Button) findViewById(R.id.button);
        }

        read = (Button) findViewById(R.id.button2);
        //colse = (Button) findViewById(R.id.button3);
//        ic = (ImageView) findViewById(R.id.imageView);
//        tv = (TextView) findViewById(R.id.textView);
        name = (TextView) findViewById(R.id.coor_custmer_name);
        sex = (TextView) findViewById(R.id.coor_custmer_de_sex);
        minzu = (TextView) findViewById(R.id.coor_custmer_de_age);
        bday = (TextView) findViewById(R.id.coor_custmer_de_number);
        adress = (TextView) findViewById(R.id.coor_customer_de_manger);
        idCard = (TextView) findViewById(R.id.coor_customer_de_gj);
        qfjg = (TextView) findViewById(R.id.coor_customer_qf_jg);
        time = (TextView) findViewById(R.id.coor_customer_data);
        ic = (ImageView) findViewById(R.id.id_img);
    }

    private void ReadCard() {
        try {
//            MyLog.i("", "start");
            if ((mInputStream == null) || (mOutputStream == null)) {
                Readflage = -2;// 连接异常
                return;
            }
            mOutputStream.write(cmd_find);
            Thread.sleep(200);
            int datalen = mInputStream.read(recData);

            if (recData[9] == -97) {
                mOutputStream.write(cmd_selt);
                Thread.sleep(200);
                datalen = mInputStream.read(recData);

                if (recData[9] == -112) {
                    mOutputStream.write(cmd_read);
                    Thread.sleep(1000);
                    byte[] tempData = new byte[1500];
                    if (mInputStream.available() > 0) {
                        datalen = mInputStream.read(tempData);
                    } else {
                        Thread.sleep(500);
                        if (mInputStream.available() > 0) {
                            datalen = mInputStream.read(tempData);
                        }
                    }

                    int flag = 0;
                    if (datalen < 1294) {
                        for (int i = 0; i < datalen; i++, flag++) {
                            recData[flag] = tempData[i];
                        }
                        Log.d("cccc1",recData.toString());
                        Thread.sleep(1000);
                        if (mInputStream.available() > 0) {
                            datalen = mInputStream.read(tempData);
                        } else {
                            Thread.sleep(500);
                            if (mInputStream.available() > 0) {
                                datalen = mInputStream.read(tempData);
                            }
                        }

                        for (int i = 0; i < datalen; i++, flag++) {
                            recData[flag] = tempData[i];
                        }

                    } else {
                        for (int i = 0; i < datalen; i++, flag++) {
                            recData[flag] = tempData[i];
                        }
                    }
                    tempData = null;

                    if (flag == 1295) {
                        if (recData[9] == -112) {

                            final byte[] dataBuf = new byte[256];
                            for (int i = 0; i < 256; i++) {
                                dataBuf[i] = recData[14 + i];
                            }
//
                            String TmpStr = new String(dataBuf, "UTF16-LE");
                            Log.d("cccc2",TmpStr);
                            TmpStr = new String(TmpStr.getBytes("UTF-8"));
                            Log.d("cccc3",TmpStr);
//                            String str = "";
//                            for (int i = 0;i<dataBuf.length;i++){
//                                str = str+dataBuf[i];
//                            }
//                            Log.d("aa",str);
                            decodeInfo[0] = TmpStr.substring(0, 15);
                            decodeInfo[1] = TmpStr.substring(15, 16);
                            decodeInfo[2] = TmpStr.substring(16, 18);
                            decodeInfo[3] = TmpStr.substring(18, 26);
                            decodeInfo[4] = TmpStr.substring(26, 61);
                            decodeInfo[5] = TmpStr.substring(61, 79);
                            decodeInfo[6] = TmpStr.substring(79, 94);
                            decodeInfo[7] = TmpStr.substring(94, 102);
                            decodeInfo[8] = TmpStr.substring(102, 110);
                            decodeInfo[9] = TmpStr.substring(110, 128);
                            if (decodeInfo[1].equals("1"))
                                decodeInfo[1] = "男";
                            else
                                decodeInfo[1] = "女";
                            try {
                                int code = Integer.parseInt(decodeInfo[2].toString());
                                decodeInfo[2] = decodeNation(code);
                            } catch (Exception e) {
                                decodeInfo[2] = "";
                            }

                            // 照片解码
                            try {
                                int ret = IDCReaderSDK.Init();
                                if (ret == 0) {
                                    byte[] datawlt = new byte[1384];
                                    byte[] byLicData = {(byte) 0x05, (byte) 0x00, (byte) 0x01, (byte) 0x00,
                                            (byte) 0x5B, (byte) 0x03, (byte) 0x33, (byte) 0x01, (byte) 0x5A,
                                            (byte) 0xB3, (byte) 0x1E, (byte) 0x00};
                                    for (int i = 0; i < 1295; i++) {
                                        datawlt[i] = recData[i];
                                    }
                                    int t = IDCReaderSDK.unpack(datawlt, byLicData);
                                    if (t == 1) {
                                        Readflage = 1;// 读卡成功
                                    } else {
                                        Readflage = 6;// 照片解码异常
                                    }
                                } else {
                                    Readflage = 6;// 照片解码异常
                                }
                            } catch (Exception e) {
                                Readflage = 6;// 照片解码异常
                            }

                        } else {
                            Readflage = -5;// 读卡失败！
                        }
                    } else {
                        Readflage = -5;// 读卡失败
                    }
                } else {
                    Readflage = -4;// 选卡失败
                }
            } else {
                Readflage = -3;// 寻卡失败
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Readflage = -99;// 读取数据异常
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            Readflage = -99;// 读取数据异常
            e.printStackTrace();
        }
    }

    private String decodeNation(int code) {
        String nation;
        switch (code) {
            case 1:
                nation = "汉";
                break;
            case 2:
                nation = "蒙古";
                break;
            case 3:
                nation = "回";
                break;
            case 4:
                nation = "藏";
                break;
            case 5:
                nation = "维吾尔";
                break;
            case 6:
                nation = "苗";
                break;
            case 7:
                nation = "彝";
                break;
            case 8:
                nation = "壮";
                break;
            case 9:
                nation = "布依";
                break;
            case 10:
                nation = "朝鲜";
                break;
            case 11:
                nation = "满";
                break;
            case 12:
                nation = "侗";
                break;
            case 13:
                nation = "瑶";
                break;
            case 14:
                nation = "白";
                break;
            case 15:
                nation = "土家";
                break;
            case 16:
                nation = "哈尼";
                break;
            case 17:
                nation = "哈萨克";
                break;
            case 18:
                nation = "傣";
                break;
            case 19:
                nation = "黎";
                break;
            case 20:
                nation = "傈僳";
                break;
            case 21:
                nation = "佤";
                break;
            case 22:
                nation = "畲";
                break;
            case 23:
                nation = "高山";
                break;
            case 24:
                nation = "拉祜";
                break;
            case 25:
                nation = "水";
                break;
            case 26:
                nation = "东乡";
                break;
            case 27:
                nation = "纳西";
                break;
            case 28:
                nation = "景颇";
                break;
            case 29:
                nation = "柯尔克孜";
                break;
            case 30:
                nation = "土";
                break;
            case 31:
                nation = "达斡尔";
                break;
            case 32:
                nation = "仫佬";
                break;
            case 33:
                nation = "羌";
                break;
            case 34:
                nation = "布朗";
                break;
            case 35:
                nation = "撒拉";
                break;
            case 36:
                nation = "毛南";
                break;
            case 37:
                nation = "仡佬";
                break;
            case 38:
                nation = "锡伯";
                break;
            case 39:
                nation = "阿昌";
                break;
            case 40:
                nation = "普米";
                break;
            case 41:
                nation = "塔吉克";
                break;
            case 42:
                nation = "怒";
                break;
            case 43:
                nation = "乌孜别克";
                break;
            case 44:
                nation = "俄罗斯";
                break;
            case 45:
                nation = "鄂温克";
                break;
            case 46:
                nation = "德昂";
                break;
            case 47:
                nation = "保安";
                break;
            case 48:
                nation = "裕固";
                break;
            case 49:
                nation = "京";
                break;
            case 50:
                nation = "塔塔尔";
                break;
            case 51:
                nation = "独龙";
                break;
            case 52:
                nation = "鄂伦春";
                break;
            case 53:
                nation = "赫哲";
                break;
            case 54:
                nation = "门巴";
                break;
            case 55:
                nation = "珞巴";
                break;
            case 56:
                nation = "基诺";
                break;
            case 97:
                nation = "其他";
                break;
            case 98:
                nation = "外国血统中国籍人士";
                break;
            default:
                nation = "";
                break;
        }
        return nation;
    }

    public void OnClick(View view) {
        switch (view.getId()){
            case R.id.button3:
                finish();
                break;
            case R.id.button4:
                Intent intent = new Intent(MainActivitycard.this, AddCoorCustomerActivity.class);
                intent.putExtra("TAG","coor");
                intent.putExtra("ly","saomiao");
                startActivity(intent);
                    finish();
        }
    }
}
